package com.senven.permission.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senven.common.entity.RolePermission;
import com.senven.common.vo.CommonResult;
import com.senven.permission.mapper.RolePermissionMapper;
import com.senven.permission.service.IRolePermissionService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限 服务实现类
 * </p>
 *
 * @author YSH
 * @since 2022-09-19
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    @Override
    @Transactional
    public CommonResult fen(Integer roleid, List<Integer> ids) {
//删除roleid 对应的权限
        QueryWrapper<RolePermission> wrapper =new QueryWrapper<>();
        wrapper.eq("role_id",roleid);
        this.remove(wrapper);
        //添加新选中的权限
        List<RolePermission> collect = ids.stream().map(item -> new RolePermission(null, roleid, item, 0, LocalDateTime.now(), LocalDateTime.now()))
                .collect(Collectors.toList());
        this.saveBatch(collect);
        return new CommonResult(2000,"修改权限成功",null);
    }
}
